<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('维度管理')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: select2-css" />
    <style>
        .layui-table-click {
            background-color:#1E9FFF;
            color:#fff;
        }
    </style>
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-md-4" style="background-color: #fff;min-height:550px"> <!--style="font-size:16px;margin-top:6px;"-->
            <table id="bootstrap-table" style="margin-top: 10px"></table>
        </div>
        <div class="col-md-8" style="padding-right: 0px">
            <div class="tabs-container">
                <ul class="nav nav-tabs">
                    <li class="active"><a data-toggle="tab" href="#tab-1" aria-expanded="true">关联基础抽取</a>
                    </li>
                    <li class=""><a data-toggle="tab" href="#tab-2" aria-expanded="false">关联指标</a>
                    </li>
                </ul>
                <div class="tab-content">
                    <div id="tab-1" class="tab-pane active">
                        <div class="panel-body">
                            <div class="btn-group-sm" role="group">
                                <a class="btn btn-success" onclick="addOds()">
                                    <i class="fa fa-plus"></i> 基础抽取添加
                                </a>
                            </div>
                            <table id="drawBaseTable" class="table table-bordered"></table>
                        </div>
                    </div>
                    <div id="tab-2" class="tab-pane">
                        <div class="panel-body">
                            <div class="btn-group-sm" role="group">
                                <a class="btn btn-success" onclick="addIndex()">
                                    <i class="fa fa-plus"></i> 指标添加
                                </a>
                            </div>
                            <table id="indexTable" class="table table-bordered"></table>
                        </div>
                    </div>
                </div>
            </div>

            <!--<div class="panel-body" style="background-color: white">
                <div class="btn-group-sm" id="toolbar" role="group">
                    <a class="btn btn-success" onclick="add()">
                        <i class="fa fa-plus"></i> 添加
                    </a>
                </div>
                <table id="dimFldsTable" class="table table-bordered"></table>
            </div>-->
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<th:block th:include="include :: select2-js" />
<script th:inline="javascript">
    var prefix = ctx + "etl/job/drawbase";
    var now_jobId,now_jobName;
    var sdSysData = [[${@baseSd.getItem('hos_sys')}]];
    function addOds() {
        if(now_jobId==undefined) {
            $.modal.alertError("请先选择定时任务");
            return ;
        }
        var options = {
            title:"添加["+now_jobName+"]基础抽取数据",
            width: "500",
            height: "600",
            url: prefix+"/add/"+now_jobId,
            callBack: baseDrawCallBack
        };
        $.modal.openOptions(options);
    }

    function addIndex() {
        if(now_jobId==undefined) {
            $.modal.alertError("请先选择定时任务");
            return ;
        }
        var options = {
            title:"添加["+now_jobName+"]指标数据",
            width: "500",
            height: "600",
            url: "/etl/jobIndex/add/"+now_jobId,
            callBack: indexCallBack
        };
        $.modal.openOptions(options);
    }

    function indexCallBack(index, layero) {
        var iframeWin = layero.find('iframe')[0];
        iframeWin.contentWindow.submitHandler(index, layero);
        loadIndexTable();
    }

    function baseDrawCallBack(index, layero) {

        var iframeWin = layero.find('iframe')[0];
        iframeWin.contentWindow.submitHandler(index, layero);
        loadDrawBaseTable();
    }

    function deleteIndex(idIndex) {
        if(now_jobId==undefined) {
            return ;
        }
        $.ajax({
            url: "/etl/jobIndex/delete?idIndex="+idIndex+"&jobId="+now_jobId,
            cache: false,
            type: 'GET',
            dataType: 'json',
            success: function (result) {
                if(result.code=="0") {
                    loadIndexTable();
                }
            }
        });
    }

    function deleteDrawBase(idDrawBase) {

        if(now_jobId==undefined) {
            return ;
        }
        $.ajax({
            url: prefix+"/delete?idDrawBase="+idDrawBase+"&jobId="+now_jobId,
            cache: false,
            type: 'GET',
            dataType: 'json',
            success: function (result) {
                if(result.code=="0") {
                    loadDrawBaseTable();
                }
            }
        });
    }

    function loadDrawBaseTable() {
        if(now_jobId==undefined) {
            return ;
        }
        $("#drawBaseTable").bootstrapTable('destroy');
        $('#drawBaseTable').bootstrapTable({
            url: prefix+"/selectByJobId/"+now_jobId,
            method: "get",
            contentType: "application/json",
            modalName: "执行数据",
            showSearch: false,
            showRefresh: true,
            showToggle: false,
            showColumns: false,
            columns: [{
                checkbox: true
            },
                {
                    field: 'idDrawBase',
                    title: '',
                    visible: false
                },
                {
                    field: 'cdTbTar',
                    title: '目标表'
                },
                {
                    field: 'na',
                    title: '名称'
                },
                {
                    field: 'sdSys',
                    title: '系统',
                    formatter: function(value, item, index) {
                        return $.table.selectSdNaByCd(sdSysData, item.sdSys);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-danger btn-xs " href="javascript:void(0)" onclick="deleteDrawBase(\'' + row.idDrawBase + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        });
    }
    function loadIndexTable() {
        if(now_jobId==undefined) {
            return ;
        }
        $("#indexTable").bootstrapTable('destroy');
        $('#indexTable').bootstrapTable({
            url: "/etl/jobIndex/selectByJobId/"+now_jobId,
            method: "get",
            contentType: "application/json",
            modalName: "执行数据",
            showSearch: false,
            showRefresh: true,
            showToggle: false,
            showColumns: false,
            columns: [{
                checkbox: true
            },
                {
                    field: 'idIndex',
                    title: '指标编码'
                },
                {
                    field: 'na',
                    title: '指标名称'
                },
                {
                    field: 'sdSys',
                    title: '系统',
                    formatter: function(value, item, index) {
                        return $.table.selectSdNaByCd(sdSysData, item.sdSys);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-danger btn-xs " href="javascript:void(0)" onclick="deleteIndex(\'' + row.idIndex + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        });
    }
    function loadJobTable() {
        $('#bootstrap-table').bootstrapTable({
            url: "/monitor/job/list",
            method:"post",
            modalName: "定时任务",
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            pagination: true,
            pageSize:15,
            search: false,
            sidePagination: 'client',
            showRefresh : true,
            onClickRow: onClickRow,
            columns: [{
                field: 'jobId',
                title: '主键',
                visible: false
            },
                {
                    field: 'jobName',
                    title: '任务名称'
                },
                {
                    field: 'cronExpression',
                    title: '表达式'
                }]
        });
    }
    function onClickRow(row, $element){
        $element.addClass('layui-table-click').siblings().removeClass('layui-table-click');
        now_jobId = row.jobId;
        now_jobName = row.jobName;
        loadDrawBaseTable();
        loadIndexTable();
    }
    $(function(){
        loadJobTable();
        loadDrawBaseTable(0);
        loadIndexTable(0);
    });
</script>
</body>
</html>